
package cn.com.pusai.common.util;

import cn.com.pusai.core.util.StringUtil;
import java.lang.reflect.*;

public class MethodUtil
{

	public MethodUtil()
	{
	}

	public static Object getMethodValue(String property, Object object)
		throws Exception
	{
		Field fields[] = object.getClass().getDeclaredFields();
		Object valueObject = null;
		for (int j = 0; j < fields.length; j++)
		{
			if (!fields[j].getName().equals(property))
				continue;
			String firstChar = fields[j].getName().substring(0, 1);
			String leaveChar = fields[j].getName().substring(1);
			String methodName = (new StringBuilder(String.valueOf(firstChar.toUpperCase()))).append(leaveChar).toString();
			try
			{
				Method meth = object.getClass().getMethod((new StringBuilder("get")).append(methodName).toString(), null);
				valueObject = meth.invoke(object, null);
			}
			catch (Exception ex)
			{
				ex.printStackTrace();
				throw ex;
			}
			break;
		}

		return valueObject;
	}

	public static String getMethodStringValue(String property, Object object)
		throws Exception
	{
		Method method = null;
		String a = property.substring(0, 1).toUpperCase();
		String b = property.substring(1);
		method = object.getClass().getMethod((new StringBuilder("get")).append(a).append(b).toString(), null);
		String value = StringUtil.trimToEmpty((String)method.invoke(object, null));
		return value;
	}

	public static void setMethodStringValue(String property, String value, Object object)
		throws Exception
	{
		String firstChar = property.substring(0, 1);
		String leaveChar = property.substring(1);
		String methodName = (new StringBuilder(String.valueOf(firstChar.toUpperCase()))).append(leaveChar).toString();
		try
		{
			Method meth = object.getClass().getMethod((new StringBuilder("set")).append(methodName).toString(), new Class[] {java.lang.String.class
			});
			meth.invoke(object, new Object[] {
				value
			});
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			throw ex;
		}
	}

	public static void setSinglePrarameterMethodValue(String property, Object value, Object object, Class name)
		throws Exception
	{
		String firstChar = property.substring(0, 1);
		String leaveChar = property.substring(1);
		String methodName = (new StringBuilder(String.valueOf(firstChar.toUpperCase()))).append(leaveChar).toString();
		try
		{
			Method meth = object.getClass().getMethod((new StringBuilder("set")).append(methodName).toString(), new Class[] {
				name
			});
			meth.invoke(object, new Object[] {
				value
			});
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			throw ex;
		}
	}

	public static Object copyProperties(Object targetObject, Object srcObject)throws Exception{
		Class targetClass;
		Class returnType;
		Object returnObject;
		Method srcGetMethods[];
		String srcGetMethodName;
		Class srcClass = null;
		targetClass = null;
		returnType = null;
		returnObject = null;
		srcGetMethods = (Method[])null;
		Method targetGetMethod = null;
		Method targetSetMethod = null;
		srcGetMethodName = null;
		String targetSetMethodName = null;
		if (srcObject == null || targetObject == null)
			return null;
		int i;
		srcClass = srcObject.getClass();
		targetClass = targetObject.getClass();
		srcGetMethods = srcClass.getMethods();
		i = 0;
		 // goto _L1_L3:
		srcGetMethodName = srcGetMethods[i].getName();
		if (srcGetMethodName.equals("getValidationKey") || !srcGetMethodName.startsWith("get") || srcGetMethodName.equals("getClass") || srcGetMethodName.equals("getServletWrapper") || srcGetMethodName.equals("getMultipartRequestHandler") || srcGetMethodName.equals("getCallback"))
			//continue; /* Loop/switch isn't completed */
		returnType = srcGetMethods[i].getReturnType();
		try
		{
			returnObject = srcGetMethods[i].invoke(srcObject, null);
		}
		catch (IllegalArgumentException e1)
		{
			//continue; /* Loop/switch isn't completed */
		}
		Class targetType = null;
		
		try
		{
			targetGetMethod = targetClass.getMethod(srcGetMethodName, null);
			targetType = targetGetMethod.getReturnType();
			targetSetMethodName = (new StringBuilder("set")).append(srcGetMethodName.substring(3)).toString();
			targetSetMethod = targetClass.getMethod(targetSetMethodName, new Class[] {
				targetType
			});
		}
		catch (NoSuchMethodException e1)
		{
			//continue; /* Loop/switch isn't completed */
		}
		try
		{
			if ((targetType.getName() == null || !targetType.getName().equalsIgnoreCase("java.util.Calendar") || returnObject == null) && returnObject != null)
				targetSetMethod.invoke(targetObject, new Object[] {
					returnObject
				});
		}
		catch (IllegalArgumentException e1)
		{
			e1.printStackTrace();
			throw e1;
		}
		catch (InvocationTargetException e1)
		{
			e1.printStackTrace();
			throw e1;
		}
		i++;

		if (i < srcGetMethods.length){ 
			//goto _L3; 
		}
		else{ 
			//goto _L2
		}

	//	break MISSING_BLOCK_LABEL_331;
		
		return targetObject;
	}
}
